<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;

class Classes extends Controller
{

    //页面分页
    public function page($page ,$size,$success,$message){
        $return = [
            'data' => '',
            'success' => $success,
            'messages' => $message,
            'page' =>$page,
            'size' => $size
        ];
        $data2 = model('Classes')->order('weight asc,update_time desc')->paginate($size, false, [
            'page' => $page,
            'var_page' => 'page'
        ]);
        $data = $data2->toArray();

        if ($page>$data['last_page']) {
            $data = model('Classes')->order('weight asc,update_time desc')->paginate($size, false, [
                'page' => $data['last_page'],
                'var_page' => 'page'
            ]);
            $data = $data->toArray();
            $return['page'] = $data['last_page'];
        }
        $classes = $data['data'];
        foreach ($classes as $k=>$v){

            $classes[$k]['count'] = Db::table('articles')->where('class_id',$v['id'])->count();

        }
        $return['data'] = $classes;
        $return['total'] = $data['total'];

        $return['page'] = $data['current_page'];
        return $return;
    }

    //添加分类
    public function addClass(){
        $page =input('post.page');
        $size = input('post.size');


            $data = [
                'name' => input('post.title'),
                'photo'=> input('post.img'),
                'sort' => 0,
                'weight' =>0

            ];
            $resault = model('Classes')->addClass($data);
            if ($resault==1){
                $return['success'] =true;
                $return['messages'] = '添加分类成功';
            }else{
                $return['messages'] =$resault;
            }

        $data2 = model('Classes')->order('weight asc,update_time desc')->paginate($size, false, [
            'page' => $page,
            'var_page' => 'page'
        ]);
        $data = $data2->toArray();

        if ($page>$data['last_page']) {
            $data = model('Classes')->order('weight asc,update_time desc')->paginate($size, false, [
                'page' => $data['last_page'],
                'var_page' => 'page'
            ]);
            $data = $data->toArray();

        }
        $return['data'] = $data['data'];
        $return['total'] = $data['total'];



        return json($return);


    }
    //删除分类
    public function deleteClass(){
        $page =input('delete.page');
        $size = input('delete.size');


        $id = input('delete.id');
        $basesInfo = model('Classes')->where('id',$id)->find();
        if (!$basesInfo){
            $return = $this->page($page,$size,false,"查无此分类");
        }else{
            $result = model('Classes')->where('id',$id)->delete();
            if ($result){

                $return = $this->page($page,$size,true,"添加成功");
            }else{
                $return = $this->page($page,$size,false,"删除失败");

            }
        }

        return json($return);

    }
    //修改分类
    public function updateClass(){
        //page,size,success,message,data
        $page =input('put.page');
        $size = input('put.size');


            $data = [
                'name' => input('put.title'),
                'photo'=> input('put.img')
            ];
            $id = input('put.id');
            $classInfo =model('Classes')->where('id',$id)->find();
            if ($classInfo){
                $result = model('Classes')->updateClass($id,$data);
                if ($result){
                    $return = $this->page($page,$size,true,"跟新成功");
                }else{
                    $return = $this->page($page,$size,false,"更新失败");
                }
            }else{
                $return = $this->page($page,$size,false,"无此分类信息");
            }


        return json($return);

    }
    //分类列表
    public function  classListe(){
        $size = input('get.size');
        $page = input('get.page');

        $return = $this->page($page,$size,true,"读取成功");

        return json($return);

    }
    //修改权重
    public function weight(){
        $size = input('put.size');
        $page = input('put.page');
        $id = input('put.id');
        $weight = input('put.weight');

        $result = model('Classes')->where('id',$id)->update(['weight'=>$weight,'sort'=>$weight]);
        if ($result){
            $return = $this->page($page,$size,true,"修改成功");

        }else{
            $return = $this->page($page,$size,false,"修改失败");

        }


        return json($return);
    }


}
